<?php
$xmlResponse = "<?xml version='1.0' encoding='ISO-8859-1'?>";
$xmlMessage="";
$xmlDoc=new DOMDocument();
$result=true;
$debug=true;

if($HTTP_RAW_POST_DATA=="")
{
	$HTTP_RAW_POST_DATA="<search><dir>/</dir></search>";
}
/*
if($debug)
{
	echo $_GET['site'];
	echo $_GET['tag'];
}
*/



function printxmlResponse($logname)
{
	global $HTTP_RAW_POST_DATA, $xmlDoc, $xmlResponse, $xmlMessage, $result, $debug;

	header("Content-Type: text/xml");
	header("Cache-Control: no-cache, must-revalidate");

	if ($xmlMessage != "")
	{
		$xmlMessage = "<message>" . $xmlMessage . "</message>";
		echo $xmlMessage;
	}
	
	echo $xmlResponse;

	$fp = fopen("./Log/".$logname."Response.xml", "w+");
	fwrite($fp, $xmlResponse);
	fwrite($fp, $xmlMessage);
	fclose($fp);
}

function printxmlRequest($logname)
{
	global $HTTP_RAW_POST_DATA, $xmlDoc, $xmlResponse, $xmlMessage, $result, $debug;

	$fp = fopen("./Log/".$logname."Request.xml", "w+");
	fwrite($fp, $HTTP_RAW_POST_DATA);
	fclose($fp);

	$err = $xmlDoc->load("./Log/".$logname."Request.xml");
}


function getFullSite($site, $link)
{
	if( strncmp($link, "http://", 7)==0 || 
		strncmp($link, "https://", 8)==0 ||
		strncmp($link, "img ", 4)==0 ||
		strncmp($link, "mailto:", 7)==0 ) {
		$absUrl   = $link;
	}
	else if( strncmp($link, "/", 1)==0) {
		$urlParts = parse_url($site);
		$hostName = $urlParts['host'];	
		$scheme   = $urlParts['scheme'];
		$absUrl   = $scheme."://".$hostName.$link;
	}
	else
		$absUrl   = $site.$link;
	return htmlspecialchars($absUrl);
}

function getUrlHost($text)
{
	$text = getNiceName($text);

	$words = explode(".", $text);
	$last_word = end($words); // "much"

	if(count($words) == 4)
		return "/".$words[2]."/".$words[1];
	else if (count($words) == 3)
	{
		if($words[0]=="www")
			return "/".$words[1];
		else
			return "/".$words[1]."/".$words[0];
	}
	else
		echo "count of words is ". count($words);
		return "/".$words[(count($words)-1)];
}

function getDirPath($text)
{

	$text = getNiceName($text);

	$words = explode("/", $text);
	$last_word = end($words); // "much"

	ucfirst($words);
        for($i = 0; $i < count($words); $i++)
	{
		$subwords = explode(" ", $words[$i]);
		for($j=0; $j < count($subwords); $j++)
			$subwords[$j] = ucfirst($subwords[$j]);
		$words[$i] = implode(" ", $subwords);
	}

	if(!strstr($last_word, ".")===false)
		array_pop($words);

	$text = implode("/", $words);

	return $text;
}

function getNiceName($lname)
{

	$lname = trim($lname);

	$patterns = array('/(\s*\:\:\s*)|(\s*\:\s*)|(\s*\|\s*)|(\s*\,\s*)|(\s\-\s)/');
	$replace = array('/');
	$lname = preg_replace($patterns, $replace, $lname);

	$patterns = array('/(\n)|(\/$)|(\')/', '/\/~/', '/_/');
	$replace = array('', '/', ' ');
	$lname = preg_replace($patterns, $replace, $lname);

	$lname = preg_replace("/[^[:alnum:][:punct:][:space]]/","",$lname); 


	return rtrim($lname, "\/");
}

function lastWord($text)
{

	echo $text."<br/>";
	$text = rtrim($text,"\/");
	echo $text."<br/>";

	$words = explode("/", $text);
	$last_word = end($words); // "much"

	if($last_word == $text)
		return "";

	if($last_word == "")
		$last_word = $words[sizeof($words)-1];
	
	$words = explode(".", $last_word);
	$last_word = current($words); // first word in filename

	return $last_word;
}

function site_get_contents($url)
{
	error_reporting(E_ALL);

	$auth = base64_encode('mcole:cole85');
	$header = "Authorization: Basic {$auth}\r\n"."Content-Type: text/html; charset=unicode\r\n";
	$hopts = array( 'request_fulluri' => true,
			'method' => 'GET',
			'header' => $header);

	$opts = array(	'http'  => $hopts,
			'https' => $hopts);
//	print_r($opts);

	$ctx = stream_context_create($opts);
	return file_get_contents($url, false,$ctx);

}

?>
